﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HappyFrogXna.Roles;
using Microsoft.Xna.Framework;

namespace HappyFrogXna.Roles
{
    /// <summary>
    /// This struct is created because we can't sure how many kind of entities we will have in the future
    /// so, thie struct can be used when passing all entities as a function paramater.
    /// </summary>
    class EntityAssemble
    {
        public EntityAssemble()
            : this(null, null, null)
        { }

        public EntityAssemble(List<Entity> ls, List<Entity> lo, List<Entity> la)
        {
            if (ls == null)
                Frogs = new List<Entity>();
            else
                Frogs = ls;

            if (lo == null)
                Objects = new List<Entity>();
            else
                Objects = lo;

            if (la == null)
                Animals = new List<Entity>();
            else
                Animals = la;
        }

        public List<Entity> Frogs;
        public List<Entity> Objects;
        public List<Entity> Animals;

        /// <summary>
        /// Test whether there is entity still moving
        /// </summary>
        /// <returns></returns>
        public bool TestEntityMoving(float sceneGroundWidthInSim)
        {
            for (int i = 0; i < Frogs.Count; ++i)
            {
                if (Frogs[i].IsMoving(sceneGroundWidthInSim))
                    return true;
            }
            for (int i = 0; i < Objects.Count; ++i)
            {
                if (Objects[i].IsMoving(sceneGroundWidthInSim))
                    return true;
            }
            for (int i = 0; i < Animals.Count; ++i)
            {
                if (Animals[i].IsMoving(sceneGroundWidthInSim))
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Test whether a positon touched a entity
        /// </summary>
        /// <param name="pos"></param>
        /// <returns></returns>
        public Entity TestTouchedEntity(Vector2 pos)
        {
            for (int i = 0; i < Frogs.Count; ++i)
            {
                if (Frogs[i].IsTouched(pos, 0))
                    return Frogs[i];
            }
            for (int i = 0; i < Objects.Count; ++i)
            {
                if (Objects[i].IsTouched(pos, 0))
                    return Objects[i];
            }
            for (int i = 0; i < Animals.Count; ++i)
            {
                if (Animals[i].IsTouched(pos, 0))
                    return Animals[i];
            }
            return null;
        }

        internal void Update(GameTime gameTime)
        {
            for (int i = 0; i < Frogs.Count; ++i)
            {
                Frogs[i].Update(gameTime);
            }
            for (int i = 0; i < Objects.Count; ++i)
            {
                Objects[i].Update(gameTime);
            }
            for (int i = 0; i < Animals.Count; ++i)
            {
                Animals[i].Update(gameTime);
            }
        }

        internal void RemoveBrokenObjects()
        {
            for (int i = 0; i < Objects.Count; ++i)
            {
                if (Objects[i].IsVisible && Objects[i].UserDate.Killed)
                    Objects[i].RemoveMe();
            }
            for (int i = 0; i < Animals.Count; ++i)
            {
                if (Animals[i].IsVisible && Animals[i].UserDate.Killed)
                    Animals[i].RemoveMe();
            }
        }
    }
}
